﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana
{
    public class SpriteReference
    {
        private Sprite sprite;
        private bool isRelease;

        ~SpriteReference()
        {
            this.Release(true);
        }

        public void SetSprite(Sprite sprite)
        {
            if (this.sprite == sprite) return;
            if(this.sprite != null)
            {
                this.Release(false);
            }

            this.sprite = sprite;
            Retain();
        }

        private void Retain()
        {
            if(this.sprite != null && !this.isRelease)
            {
                this.isRelease = SpritePool.Instance.Retain(this.sprite);
            }
        }

        private void Release(bool isrelease)
        {
            if(this.isRelease && this.sprite != null)
            {
                this.isRelease = false;
                if (isrelease)
                {
                   // ReferenceDict.AddNeedClearSprite(this.sprite);
                }
                else
                {
                    SpritePool.Instance.Free(this.sprite, false);
                }

                this.sprite = null;
            }
        }


    }
}

